import * as contract from '../../services/contract';
import {message} from 'antd';

export default {
  namespace: 'contract',
  state: {
    list: [],
    loading:false,
    current:1,
    currentSize:10,
    total:0,
    modalVisible:false,
    buttonLoading:false,
    modalType:'create',
    newKey : '',
    currentItem:{},
    searchObj:{},
    selectedRowKeys:[],
    buttonVisible:true,
    modalViewVisible:false,
  },
  reducers: {
    updateState(state,{payload}){
      return { ...state, ...payload,};
    },
    showLoading(state, { payload }) {
      return { ...state, ...payload,loading:true };
    },
    hideLoading(state, { payload }) {
      return { ...state, ...payload,loading:false };
    },
    showButtonLoading(state,{payload}){
      return { ...state, ...payload,buttonLoading:true };
    },
    hideButtonLoading(state, { payload }) {
      return { ...state, ...payload,buttonLoading:false };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    showModal(state,{payload}){
      return {...state,...payload,modalVisible:true};
    },
    hideModal(state,{payload}){
      return {...state,...payload,modalVisible:false,buttonLoading:false};
    },
    showViewModal(state,{payload}){
      return {...state,...payload,modalViewVisible:true};
    },
    hideViewModal(state,{payload}){
      return {...state,...payload,modalViewVisible:false};
    },
  },
  effects: {
    *queryList({ payload },{ call, put }) {
      yield put({ type: 'showLoading' });
      const { body } = yield call(contract.queryList,payload);
      yield put({ type: 'queryListSuc',
        payload: { list:body.data, current:body.current,
            currentSize:body.size,
            total:body.iTotalRecords,
            searchObj:payload,
            buttonVisible:true,
            selectedRowKeys:[]}});
      yield put({ type: 'hideLoading' });
    },
    *create({ payload,searchObj,form},{ call, put }){
      yield put({ type: 'showButtonLoading' });
      const { body } = yield call(contract.create,payload);
      if(body.httpCode==200){
        yield put({ type: 'hideModal' });
        form.resetFields();
        yield put({ type: 'queryList',payload:searchObj });
        yield put({ type: 'hideButtonLoading' });
      }else{
        yield put({ type: 'hideButtonLoading' });
      }
    },
    *qryById({ payload },{ call, put }){
      yield put({ type: 'showLoading' });
      const { body} = yield call(contract.getConcatInfo,payload);
        yield put({type: 'showModal',
        payload:{currentItem:body.data,modalType:'update'}});
      yield put({ type: 'hideLoading' });
    },
    *update({ payload,searchObj,form},{ call, put }){
      yield put({ type: 'showButtonLoading' });
      const { body } = yield call(contract.update,payload);
      if(body.httpCode==200){
        yield put({ type: 'hideModal' });
        form.resetFields();
        yield put({ type: 'queryList',payload:searchObj });
        yield put({ type: 'hideButtonLoading' });
      }else{
        yield put({ type: 'hideButtonLoading' });
      }
    },
    *remove({ payload,searchObj},{ call, put }){
      yield put({ type: 'showLoading' });
      const { body } = yield call(contract.remove,payload);
      yield put({ type: 'queryList',payload:searchObj });
    },
    *qryDetailById({ payload },{ call, put }){
      yield put({ type: 'showLoading' });
      const { body} = yield call(contract.getConcatInfo,payload);
        yield put({type: 'showViewModal',
        payload:{currentItem:body.data,}});
      yield put({ type: 'hideLoading' });
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/contract') {
          dispatch({ type: 'queryList', payload:{'pageNum':1,'pageSize':10}});
        }
      });
    },
  },
};
